package com.cgycms.webdeploy.log.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.cgycms.webdeploy.filter.SpringContextHolder;
import com.cgycms.webdeploy.log.LogModel;
import com.cgycms.webdeploy.log.LogService;
import com.cgycms.webdeploy.storage.DeployConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @ClassName : LogServiceFileImpl
 * @Description : 日志信息写入至文件
 * @Author : 王力超
 * @Date: 2020-11-11 15:31
 */
@Component
public class LogServiceFileImpl implements LogService {

    private static Logger log = LoggerFactory.getLogger(LogServiceFileImpl.class);

    @Override
    public void consumeLog(LogModel model) {
        DeployConfig deployConfig = SpringContextHolder.getBean(DeployConfig.class);
        String logPath = deployConfig.getLogPath();
        if (StrUtil.isBlank(logPath)) {
            log.warn("已开启日志记录功能,未配置存储位置！");
            return;
        }
        FileUtil.appendUtf8String(JSONUtil.toJsonStr(model) + "\n", logPath);
    }

}
